<template>
  <div class="profile">
    <profile-background @tabClick="tabClick"/>

    <scroll class="scroll-content"
      ref="scroll"
      :pull-up-load="true">
      <!-- 全部栏 -->
      <div class="content" v-if="this.categoryIndex === 0">
        <div class="content-title">
          <span>数据更新于{{nowTime}}</span>
          <div class="content-type">
            <div class="clicked" ref="clicked"></div>
            <div @click="itemClick(0)">本周</div>
            <div @click="itemClick(1)">本月</div>
            <div @click="itemClick(2)">全年</div>
          </div>
        </div>
        <!-- 可视化图表 -->
        <data-table-bar v-if="this.currentIndex === 0"/>
        <data-table-pie v-else-if="this.currentIndex === 1"></data-table-pie>
        <data-table-line v-else-if="this.currentIndex === 2"></data-table-line>
      </div>

      <!-- 分类栏 -->
      <div class="content" v-else>
        <div class="content-title">
          <span>数据更新于{{nowTime}}</span>
        </div>
        <!-- 可视化图表 -->
        <data-table-other
        :titles="titles"
        :categoryIndex="categoryIndex"
        ref="tableOther"/>
      </div>
    </scroll>
  </div>
</template>

<script>
import profileBackground from './child_comps/profile_background.vue'
import dataTableBar from './child_comps/data_table_bar.vue'
import dataTablePie from './child_comps/data_table_pie.vue'
import dataTableLine from './child_comps/data_table_line.vue'
import dataTableOther from './child_comps/data_table_other.vue'

import scroll from 'components/common/scroll/scroll'

export default {
  name: "profile",
  components: {
    profileBackground,
    dataTableBar,
    dataTablePie,
    dataTableLine,
    dataTableOther,
    scroll
  },
  data() {
    return {
      nowTime: '2022/03/10',
      categoryIndex: 0, // 类目：全部/家纺等
      currentIndex: 0, // 本周/本月/全年
      titles: [] // profile_background 里定义的titles，传入 data_table_other
    }
    
  },
  mounted() {
    var _this = this;
    let yy = new Date().getFullYear();
    let mm = new Date().getMonth()+1;
    let dd = new Date().getDate();
    let hh = new Date().getHours();
    let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
    let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
    _this.gettime = yy+'/'+mm+'/'+dd+' '+hh+':'+mf+':'+ss;
    this.nowTime = _this.gettime;
  },
  methods: {
    // 切换本周/本月/全年
    itemClick(index) {
      this.$refs.clicked.style.left = Number((index * 10.628).toFixed(3)) + "vw";
      this.currentIndex = index;
    },
    // 切换类目
    tabClick(index, titles) {
      this.categoryIndex = index;
      this.titles = titles;
      this.currentIndex = 0;
      if(this.categoryIndex) {
        this.$nextTick(function() {
          this.$refs.tableOther.tableOtherUpdate();
        })
      }
    }
  }
}
</script>

<style scoped>
  .profile {
    height: 100vh;
    position: relative;
    overflow: hidden;
  }

  .scroll-content {
    position: absolute;
    top: 160px;
    right: 0;
    bottom: 49px;
    left: 0;
    margin-top: -28px;
  }

  .content {
    width: 100%;
    height: auto;
    background-color: #fff;
    border-radius: 20px;
  }

  .content-title {
    width: 100%;
    height: 60px;
    line-height: 60px;
    padding: 0 10px 0 20px;
  }

  .content-title span {
    font-size: 10px;
    color: #bbb;
  }

  .content-title .content-type {
    width: 138px;
    height: 30px;
    margin: 15px 0;
    padding: 0 2px;
    border-radius: 15px;
    background-color: rgb(245, 245, 245);
    float: right;
    display: flex;
    line-height: 30px;
    font-size: 14px;
    position: relative;
    z-index: 0;
  }

  .content-title .content-type div {
    flex: 1;
    text-align: center;
  }

  .content-title .content-type .clicked {
    width: 46px;
    height: 26px;
    margin: 2px;
    background-color: #fff;
    border-radius: 15px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
    transition: left 0.2s ease-out;
  }

</style>